package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.commons.utils.JsonReader;
import com.ruoyi.system.domain.Tool;
import com.ruoyi.system.domain.ToolKit;
import com.ruoyi.system.domain.dto.ToolJsonDto;
import com.ruoyi.system.domain.dto.ToolJsonResponseDto;
import com.ruoyi.system.mapper.ToolMapper;
import com.ruoyi.system.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author QiuYuchun
 * Date: 2023/12/20
 * Time: 11:23
 */

@Service
public class ToolServiceImpl extends ServiceImpl<ToolMapper, Tool> implements ToolService {

    @Autowired
    ToolService toolService;

    @Autowired
    JsonReader jsonReader;

    @Override
    public List<Tool> getToolByToolkitName(String tkName) {
        LambdaQueryWrapper<Tool> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tool::getTools,tkName);
        queryWrapper.eq(Tool::getIsDeleted,0);
        List<Tool> list = this.list(queryWrapper);
        if (list == null && list.size() == 0){
            throw new ServiceException("找不到对应工具集");
        }
        return list;
    }

    @Override
    public ToolJsonResponseDto getToolJsonByToolName(String toolName){
        LambdaQueryWrapper<Tool> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tool::getResourceName,toolName);
        queryWrapper.eq(Tool::getIsDeleted,0);
        Tool tool = toolService.getOne(queryWrapper);
        String toolJsonFileUrl = tool.getJsonFileUrl();
        ToolJsonResponseDto toolJsonResponseDto = jsonReader.read(toolJsonFileUrl);
        return toolJsonResponseDto;
    }
}
